package com.ece.pfe_rover_pvt_ines_ece_application.view;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import com.ece.pfe_rover_pvt_ines_ece_application.PFE_ROVER_PVT_INES_ECE_Application;
import com.ece.pfe_rover_pvt_ines_ece_application.model.Measurements;
import com.ece.pfe_rover_pvt_ines_ece_application.model.SimpleMeasurement;
import com.parse.ParseException;
import com.parse.ParseQuery;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.Gravity;
import android.widget.Toast;

public class LumGraph {
	
	public boolean DEBUG_MODE = PFE_ROVER_PVT_INES_ECE_Application.DEBUG;
	private List<Measurements> list1 = null, list2 = null;	
						
	private GregorianCalendar gc1 = new GregorianCalendar();
	private GregorianCalendar gc2 = new GregorianCalendar();
	private long dateFilter;
	private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
	
	
	public Intent getIntent(Context context, long df) {
		
		dateFilter = df;
		
		//Panel 1	
		list1 = populateTableQuery(1);		
		//Panel 2
		list2 = populateTableQuery(2);


		double[] array_lum = new double[list1.size()];
		Date[] array_time = new Date[list1.size()];

		double[] array_lum2 = new double[list2.size()];
		Date[] array_time2 = new Date[list2.size()];

		for (int i = 0; i < list1.size(); i++) {
			double values_lum = list1.get(i).getLum();
			array_lum[i] = values_lum; // on mets toutes les valeurs de I dans
										// un tableau de double
			System.out.println("Test" + array_lum[i]);

			long date1 = list1.get(i).getDate();
			gc1.setTimeInMillis(date1);
			Date values_time = gc1.getTime();					
			
			array_time[i] = values_time;// on mets toutes les valeurs de
												// TimeStamp dans un tableau de
												// double

		}

		TimeSeries series1 = new TimeSeries(
				"Luminosité en fonction du temps pour PV1");

		for (int i = 0; i < array_time.length; i++) {
			series1.add(array_time[i], array_lum[i]);

		}

		for (int i = 0; i < list2.size(); i++) {
			double values_lum2 = list2.get(i).getLum();
			array_lum2[i] = values_lum2; // on mets toutes les valeurs de I dans
											// un tableau de double

			long date2 = list2.get(i).getDate();
			gc2.setTimeInMillis(date2);
			Date values_time2 = gc2.getTime();
			
			array_time2[i] = values_time2;// on mets toutes les valeurs de
												// TimeStamp dans un tableau de
												// double

		}
		TimeSeries series2 = new TimeSeries(
				"Luminosité en fonction du temps pour PV2");

		for (int i = 0; i < array_time2.length; i++) {
			series2.add(array_time2[i], array_lum2[i]);
		}

		XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
		dataset.addSeries(series1);
		dataset.addSeries(series2);

		XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); // Holds
																				// a
																				// collection
																				// of
																				// XYSeriesRenderer
																				// and
																				// customizes
																				// the
																				// graph
		XYSeriesRenderer renderer = new XYSeriesRenderer(); // This will be used
															// to customize line
															// for PV1
		XYSeriesRenderer renderer2 = new XYSeriesRenderer();// This will be used
															// to customize line
															// for PV2

		// Customization time for IDPV1!
		renderer.setColor(Color.WHITE);
		renderer.setPointStyle(PointStyle.SQUARE);
		renderer.setFillPoints(true);
		renderer.setLineWidth(3);
		renderer.setChartValuesSpacing(20);
		renderer.setChartValuesTextSize(12);		

		// Customization time for IDPV2!
		renderer2.setColor(Color.GREEN);
		renderer2.setPointStyle(PointStyle.SQUARE);
		renderer2.setFillPoints(true);
		renderer2.setLineWidth(3);
		renderer2.setChartValuesSpacing(20);
		renderer2.setChartValuesTextSize(12);
		
		renderer.setDisplayChartValues(true);
		renderer2.setDisplayChartValues(true);		
		
		mRenderer.addSeriesRenderer(renderer);
		mRenderer.addSeriesRenderer(renderer2);
		mRenderer
				.setChartTitle("Luminosité du PV1 et PV2 en fonction du temps");
		mRenderer.setXTitle("Timestamp");
		mRenderer.setYTitle("Luminosité");

		Intent intent = ChartFactory.getTimeChartIntent(context, dataset,
				mRenderer, "Luminosité en fonction du temps");
		
		Toast toast = null;
		if(list1.size() == 0) {
			toast = Toast.makeText(context, "Désolé, il n'y a aucune mesure correspondant"
					+ " à votre recherche ", Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.CENTER, 0, 0);
			toast.show();
		}
		return intent;
	}
	
	/**
	 * Query the local datastore
	 * Return all the measurements present in the file 
	 */
	private List<Measurements> populateTableQuery(int idPan) {
		List<Measurements> list = new ArrayList<Measurements>();
		
		ParseQuery<Measurements> query = ParseQuery.getQuery(Measurements.class);
		query.fromLocalDatastore();
		query.selectKeys(Arrays.asList("Lum", "Date"));
		query.whereEqualTo("IdPan", idPan);
		query.whereGreaterThanOrEqualTo("Date", this.dateFilter);
		query.orderByDescending("EntryId");
				
		try {
			list = query.find();
		} catch (ParseException e) {
			if(DEBUG_MODE) System.out.println("Query has not found anything");
			e.printStackTrace();
		}		
		return list;
	}
}
